package com.yin.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Demo01 {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();// 单个线程

        try {
            System.out.println("______________________________________");
            for (int i = 0; i < 10; i++) {
                // 使用了线程池之后要使用线程池来创建线程
                executorService.execute(()-> System.out.println(Thread.currentThread().getName()));

            }
        } finally {
            // 使用完线程池之后关闭
            executorService.shutdown();
        }

        ExecutorService executorService1 = Executors.newFixedThreadPool(5);// 固定大小的线程池
        try {
            System.out.println("______________________________________");
            for (int i = 0; i < 10; i++) {
                // 使用了线程池之后要使用线程池来创建线程
                executorService1.execute(()-> System.out.println(Thread.currentThread().getName()));

            }
        } finally {
            // 使用完线程池之后关闭
            executorService1.shutdown();
        }


        ExecutorService executorService3 = Executors.newCachedThreadPool();// 可伸缩的,遇强则强,遇弱则弱

        try {
            System.out.println("______________________________________");
            for (int i = 0; i < 100000; i++) {
                executorService3.execute(()-> System.out.println(Thread.currentThread().getName()));
            }
        }finally {
            executorService3.shutdown();
        }

    }
}
